package com.ruoyi.scrap.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.scrap.domain.ScrapApplyAudit;
import com.ruoyi.scrap.domain.vo.ScrapApplyAuditVo;
import com.ruoyi.scrap.mapper.ScrapApplyAuditMapper;
import com.ruoyi.scrap.service.ScrapApplyAuditService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ScrapApplyAuditServiceImpl extends ServiceImpl<ScrapApplyAuditMapper, ScrapApplyAudit> implements ScrapApplyAuditService {

    @Resource
    ISysPostService postService;

    @Override
    public List<ScrapApplyAudit> getScrapApplyAudit(ScrapApplyAudit audit) {
        return baseMapper.getScrapApplyAudit(audit);
    }

    @Override
    @Transactional
    public void audit(ScrapApplyAuditVo auditVo, SysUser loginUser) {

        ScrapApplyAudit audit = new ScrapApplyAudit();

        audit.setApplyId(auditVo.getApplyId());
        audit.setAuditor(loginUser.getUserId());
        audit.setAuditBy(loginUser.getNickName());
        audit.setAuditTime(new Date());
        audit.setAuditIdea(auditVo.getReason());
        audit.setAuditStatus(auditVo.getAuditStatus());
        audit.setAuditSign(auditVo.getSign());
        audit.setAuditDept(loginUser.getDeptId());
        audit.setAuditDeptName(loginUser.getDept().getDeptName());

        if(!ObjectUtils.isEmpty(loginUser.getPostIds())){

            audit.setAuditPost(loginUser.getPostIds()[0]);

            SysPost post = postService.selectPostById(loginUser.getPostIds()[0]);

            if(!ObjectUtils.isEmpty(post)){
                audit.setAuditPostName(post.getPostName());
            }

        }

        baseMapper.insert(audit);

    }

}
